package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.pb.activity.jiuzhou.JiuZhouProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_JIU_ZHOU_SUPPORT_FROM_CROSS, desc = "声援")
public class JiuZhouSupportCmdFromCross extends AbstractCommand {


    private String reward = "";


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossJiuZhouProto.CrossJiuZhouSupportRespMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouSupportRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        if (reqMsg.hasReward()) {
            reward = reqMsg.getReward();
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_JIU_ZHOU_CONFIG_NOT_EXIST;
            return;
        }

        Property cost = new Property(config.getMoneyItemId3(), 1);
        // 取消预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(cost);
        if (ret == 0) {
            // 扣除道具
            player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouSupportCost);

            // 获得道具
            if (!StringUtils.isNullOrEmpty(reward)) {
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.JiuZhou, eLogMoneyType.JiuZhouSupportGet);
            }
        }
        JiuZhouProto.JiuZhouSupportRespMsg.Builder respMsg = JiuZhouProto.JiuZhouSupportRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (!StringUtils.isNullOrEmpty(reward)) {
            respMsg.setReward(reward);
        }
        player.sendPacket(ClientProtocol.U_JIU_ZHOU_SUPPORT, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
